package com.tanhua.sso.controller;


import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 完善个人信息-基本信息
     *
     * @param param
     * @return
     */
    @RequestMapping("/loginReginfo")
    public ResponseEntity<Object> loginReginfo(@RequestBody Map<String,String> param,
                                               @RequestHeader("Authorization") String token){
        try {
            boolean flag = userInfoService.loginReginfo(param,token);
            if (flag)
                return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("00003").errMessage("用户信息保存失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 完善个人信息-用户头像
     *
     * @return
     */
    @RequestMapping("/loginReginfo/head")
    public ResponseEntity<Object> saveUserLogo(MultipartFile headPhoto,
                                               @RequestHeader("Authorization") String token){
        try {
            boolean flag = userInfoService.saveUserLogo(headPhoto,token);
            if (flag)
                return ResponseEntity.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("00004").errMessage("用户信息头像上传失败").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
